Page({
  data: {
    mode: 'rectangular',
    length: 0,
    width: 0,
    height: 0,
    radius: 0,
    cylindricalHeight: 0,
    materials: ['6061', '7075', '304', '303', '316L', 'Q235', '45#', 'DT4C', 'S45C', '镀铬棒', 'SKD（模具钢）', '锌合金', '黄铜', 's136', 'peek', '锡青铜', '5051', 'A3', '红铜', 'Cr12MoV', 'pp', '赛钢POM', '铁氟龙', 'PA'],
    selectedMaterial: '',
    densities: { '6061': 2.7, '7075': 2.85, '304': 7.93, '303': 7.95, '316L': 7.98, 'Q235': 7.96, '45#': 7.85, 'DT4C': 7.85, 'S45C': 6.17, '镀铬棒': 7.19, 'SKD（模具钢）': 7.85, '锌合金': 7.85, '黄铜': 8.5, 's136': 7.75, 'peek': 2.2, '锡青铜': 8.9, '5051': 7.85, 'A3': 7.85, '红铜': 8.9, 'Cr12MoV': 7.85, 'pp': 1, '赛钢POM': 1.5, '铁氟龙': 2.2, 'PA': 1.15 },
    prices: { '6061': 25, '7075': 30, '304': 25, '303': 28, '316L': 38, 'Q235': 8, '45#': 7.5, 'DT4C': 20, 'S45C': 6.8, '镀铬棒': 6.8, 'SKD（模具钢）': 20, '锌合金': 41, '黄铜': 52, 's136': 30, 'peek': 750, '锡青铜': 150, '5051': 7.5, 'A3': 7.5, '红铜': 70, 'Cr12MoV': 16, 'pp': 20, '赛钢POM': 30, '铁氟龙': 70, 'PA': 30 },
    volume: 0,
    mass: 0,
    materialCost: 0,
    difficulty: 0,
    surfaceCost: 0,
    standardPrice: 0,
    quantity: 1,
    totalPrice: 0
  },
  modeChange(e) {
    this.setData({ mode: e.detail.value });
  },
  inputChange(e) {
    let field = e.currentTarget.dataset.field;
    this.setData({ [field]: e.detail.value }, this.calculate);
  },
  materialChange(e) {
    let selectedMaterial = this.data.materials[e.detail.value];
    this.setData({ selectedMaterial }, this.calculate);
  },
  calculate() {
    let { mode, length, width, height, radius, cylindricalHeight, selectedMaterial, densities, prices, difficulty, surfaceCost, quantity } = this.data;
    let volume = 0, mass = 0, materialCost = 0, standardPrice = 0, totalPrice = 0;

    if (mode === 'rectangular') {
      volume = (length * width * height) / 1000;
    } else if (mode === 'cylindrical') {
      volume = (Math.PI * Math.pow(radius, 2) * cylindricalHeight) / 1000;
    }

    if (selectedMaterial) {
      let density = densities[selectedMaterial];
      let price = prices[selectedMaterial];
      mass = volume * density / 1000;
      materialCost = mass * price;
      standardPrice = materialCost * difficulty + mass * surfaceCost;
      totalPrice = standardPrice * quantity;
    }

    this.setData({ volume, mass, materialCost, standardPrice, totalPrice });
  },

  onLoad(options) {
    if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      selected: "04"
    })
	}
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }


});
